package com.rocksdata.mc.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ctz.commonframe.ui.BaseActivity;
import com.ctz.commonframe.ui.view.NoScrollViewPager;
import com.ctz.commonframe.ui.widget.ViewPagerFragmentAdapter;
import com.rocksdata.mc.ui.fragment.StudentContactListFragment;
import com.rocksdata.mc.Constant;
import com.rocksdata.mc.R;
import com.rocksdata.mc.ui.fragment.TeacherContactListFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

/**
 * Created by 陈堂正 on 2017/3/18.
 */
@ContentView(R.layout.activity_contacts)
public class ContactActivity extends BaseActivity {
	@ViewInject(R.id.tv_teacher)
	private TextView tvTeacher;
	@ViewInject(R.id.tv_student)
	private TextView tvStudent;
	@ViewInject(R.id.vp_contact)
	private NoScrollViewPager vpContact;
    @ViewInject(R.id.tv_confirm)
    private TextView tvConfirm;
    @ViewInject(R.id.et_search)
    private EditText etSearch;

	private List<Fragment> fragmentList = new ArrayList<>();
    private TeacherContactListFragment fragment0;
    private StudentContactListFragment fragment1;
    protected boolean isMultiselectMode;
    private ArrayList<String> selectedUserList;
    private int maxCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        isMultiselectMode = intent.getBooleanExtra(Constant.EXTRA_KEY_MULTISELECT_MODE, false);
        selectedUserList = intent.getStringArrayListExtra(Constant.EXTRA_KEY_MULTISELECT_USER_LIST);
        maxCount = intent.getIntExtra("max_count", 0);
		initView();
	}

    @Event(value = {R.id.tv_teacher, R.id.tv_student, R.id.iv_back, R.id.tv_confirm}, type = View.OnClickListener.class)
    private void onClick(View view) {
		switch (view.getId()) {
            case R.id.tv_teacher:
                tvTeacher.setSelected(true);
                tvStudent.setSelected(false);
                vpContact.setCurrentItem(0, false);
                break;
            case R.id.tv_student:
                tvTeacher.setSelected(false);
                tvStudent.setSelected(true);
                vpContact.setCurrentItem(1, false);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_confirm:
                ArrayList<String> idList = fragment0.getSelectedStaffNums();
                idList.addAll(fragment1.getSelectedStudentNums());
                if(maxCount > 0 ){
                    int count = selectedUserList == null ? idList.size() : selectedUserList.size() + idList.size();
                    if(count > maxCount) {
                        showToast("不能超过" + maxCount + "人");
                        return;
                    }
                }
                Intent intent = new Intent();
                intent.putStringArrayListExtra(Constant.EXTRA_KEY_MULTISELECT_USER_LIST, idList);
                setResult(RESULT_OK, intent);

//                    List<UserInfo> list = new ArrayList<UserInfo>();
//                    for(Fragment fragment: fragmentList){
//                        StudentContactListFragment contactListFragment = (StudentContactListFragment) fragment;
//                        list.addAll(contactListFragment.getSelectedUserList());
//                    }
//                    if(list.size() > 0) {
//                        Intent intent = new Intent();
//                        intent.putExtra(Constant.EXTRA_KEY_MULTISELECT_USER_LIST, (Serializable) list);
//                        setResult(RESULT_OK, intent);
//                    }else{
//                        setResult(RESULT_CANCELED);
//                    }
                finish();
                break;
		}
	}

	private void initView() {
        if(isMultiselectMode){
            tvConfirm.setVisibility(View.VISIBLE);
        }

		fragment0 = new TeacherContactListFragment();
		Bundle data0 = new Bundle();
		data0.putInt(Constant.EXTRA_KEY_CONTACT_TYPE, 0);
        data0.putBoolean(Constant.EXTRA_KEY_MULTISELECT_MODE, isMultiselectMode);
        if(selectedUserList != null && selectedUserList.size() > 0)
            data0.putStringArrayList(Constant.EXTRA_KEY_MULTISELECT_USER_LIST, selectedUserList);
		fragment0.setArguments(data0);
		fragmentList.add(fragment0);
		fragment1 = new StudentContactListFragment();
		Bundle data1 = new Bundle();
		data1.putInt(Constant.EXTRA_KEY_CONTACT_TYPE, 1);
        data1.putBoolean(Constant.EXTRA_KEY_MULTISELECT_MODE, isMultiselectMode);
        if(selectedUserList != null && selectedUserList.size() > 0)
            data0.putSerializable(Constant.EXTRA_KEY_MULTISELECT_USER_LIST, selectedUserList);
        fragment1.setArguments(data1);
		fragmentList.add(fragment1);
        vpContact.setOffscreenPageLimit(fragmentList.size());
		vpContact.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList));
        tvTeacher.setSelected(true);
        tvStudent.setSelected(false);
        vpContact.setCurrentItem(0, false);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                for(OnSearchResultListener listener: onSearchResultListenerList){
                    listener.onSearchResult(s);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
	}

	public interface OnSearchResultListener{
        void onSearchResult(CharSequence charSequence);
    }

    private List<OnSearchResultListener> onSearchResultListenerList = new ArrayList<OnSearchResultListener>();

    public void setOnSearchResultListener(OnSearchResultListener listener){
        onSearchResultListenerList.add(listener);
    }
}
